package com.bjsxt.controller;

import com.bjsxt.pojo.Admin;
import com.bjsxt.service.AdminServer;
import com.bjsxt.utils.MD5Utils;
import com.bjsxt.utils.MapControl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminServer adminServer;

    @RequestMapping(value = "/create")
    @ResponseBody
    public String create(Admin admin){
        int result = adminServer.create(admin);
        if (result<=0){
            return "error";
        }
        return "success";
    }

    @PostMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(String ids){
        int i = adminServer.deleteBatch(ids);
        if (i<=0){
            return MapControl.getInstance().error().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }


//    @RequestMapping(value = "/update",produces = "text/html;charset=utf-8")
//    @ResponseBody
//    public String update(Admin admin){
//        int result = adminServer.update(admin);
//        if (result<=0){
//            return "失败";
//        }
//        return "正确";
//    }

    @PostMapping("/update")
    @ResponseBody
    public Map<String,Object> update(@RequestBody Admin admin){
        int update = adminServer.update(admin);
        if (update<=0){
            return MapControl.getInstance().error().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }


    @PostMapping("/query")
    @ResponseBody
    public Map<String,Object> query(@RequestBody Admin admin){
        System.out.println(admin.getLimit());
        System.out.println(admin.getPage());
        System.out.println(admin.getAccount());
        System.out.println(admin.getName());
        System.out.println(admin.getPhone());
        //不传参即查询全部用户数据
        List<Admin> data = adminServer.query(admin);
        //不传参即查询用户总数
        int count = adminServer.count(admin);
        return MapControl.getInstance().page(data,count).getMap();
    }


   @GetMapping("/detail")
    public String detail(Admin admin, ModelMap modelMap) {

            Admin ad = adminServer.detail(admin);
            modelMap.addAttribute("admin",ad);
            return "admin/update";
    }


    @GetMapping("/list")
    public String list(){
        return "admin/list";
    }

    @GetMapping("/add")
    public String v_add(){
        return "admin/add";
    }

    /**
     * 用户管理中实现添加用户操作
     * @param admin
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Map<String,Object> add(@RequestBody Admin admin){
        admin.setPassword(MD5Utils.getMD5(admin.getPassword()));
        int result = adminServer.create(admin);
        if (result<=0){
            return MapControl.getInstance().error().getMap();
        }

        return MapControl.getInstance().success().getMap();
    }




}